<template>
	<view class="changeMyInfo padding">
		<h3>修改个人信息</h3>
		<form>
			<view class="cu-bar bg-white margin-top">
				<view class="action">
					修改头像
				</view>
			</view>
			<view class="cu-form-group">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
					 <image :src="imgList[index]" mode="aspectFill"></image>
						<<!-- view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view> -->
					</view>
					<view class="solids" @tap="ChooseImage" v-if="imgList.length<1">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>
			<view class="cu-form-group margin-top">
				<view class="title">昵称：</view>
				<input placeholder="请输入昵称" v-model="nickname" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">出生日期</view>
				<picker mode="date" v-model="date" :value="date" start="1920-09-01" end="2020-09-01" @change="DateChange">
					<view class="picker">
						{{date}}
					</view>
				</picker>
			</view>
			<view class="cu-form-group">
				<view class="title">电话：</view>
				<input placeholder="请输入电话" v-model="mobile" name="input"></input>
			</view>
		</form>
		<view class="saveinfo" style="text-align: center;">
			<button class="cu-btn round bg-green shadow margin-top-xl" @click="submitInfo">保存</button>
		</view>
	</view>
</template>

<script>
	export default{
		data() {
			return {
				date: '2018-12-25',
				imgList: [],
				nickname:'',
				birthday:'',
				mobile:'',
			}
		},
		onLoad() {
			this.getUserInfoData()
		},
		methods:{
			DateChange(e) {
				this.date = e.detail.value
			},
			ChooseImage() {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(res.tempFilePaths)
						} else {
							this.imgList = res.tempFilePaths
						}
						const Authorization = `JWT ${uni.getStorageSync('sessionId')}`;
						 uni.uploadFile({
							url: 'http://peixun.gaojer.com:9876/trains/update_user/',
							header:{
							  "Authorization": Authorization,
							},
							filePath: this.imgList[0],
							name: 'image',
							success: (uploadFileRes) => {
								uni.showToast({
									title:'上传成功'
								})
							}
						});
					}
				});
			},
			DelImg(e) {
				uni.showModal({
					content: '确定要取消修改吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			async getUserInfoData(){
				let res = await this.$myRuquest({
					url:'/user_info/'
				})
				this.nickname = res.data.nickname
				this.date = res.data.birthday
				this.mobile = res.data.mobile
			},
			submitInfo(){
				this.$myRuquest({
					url:'/update_user/',
					method:'POST',
					data:{nickname:this.nickname,birthday:this.date,mobile:this.mobile}
				}).then(res=>{
					uni.showToast({
						title:'提交成功'
					})
					this.getUserInfoData()
				})
			}
		}
	}
</script>

<style lang="scss">
.changeMyInfo{
	
}
</style>
